//
//  NSString+YCRichText.m
//  YCBaseModule
//
//  Created by Apple on 2022/11/1.
//

#import "NSString+YCRichText.h"
#import "YCAttributedStringConfig.h"

@implementation NSString (YCRichText)

- (NSMutableAttributedString *)createAttributedStringAndConfig:(NSArray *)configs {
    NSMutableAttributedString *attributedString = \
    [[NSMutableAttributedString alloc] initWithString:self];
    
    [configs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        YCAttributedStringConfig *oneConfig = obj;
        [attributedString addAttribute:oneConfig.attribute
                                 value:oneConfig.value
                                 range:oneConfig.range];
    }];
    
    return attributedString;
}

- (NSRange)rangeFrom:(NSString *)string {
    return [string rangeOfString:self];
}

- (NSRange)range {
    return NSMakeRange(0, self.length);
}


- (NSMutableAttributedString *)attributedString:(NSString *)string subString:(NSString *)subString {
    NSRange totalRange  = [string range];
    NSRange highRangeOne = [string rangeOfString:subString];
    return [string createAttributedStringAndConfig:@[
        [YCAttributedStringConfig font:[UIFont systemFontOfSize:12] range:totalRange],
        [YCAttributedStringConfig foregroundColor:[UIColor colorWithRed:72/255.0 green:78/255.0 blue:94/255.0 alpha:1.0] range:totalRange],
        [YCAttributedStringConfig font:[UIFont systemFontOfSize:12] range:highRangeOne],
        [YCAttributedStringConfig foregroundColor:[UIColor colorWithRed:215/255.0 green:0/255.0 blue:0/255.0 alpha:1.0] range:highRangeOne]
    ]];
}

/// 创建富文本
/// @param textColor 默认文字颜色
/// @param textFont 默认字体
/// @param optionString 需要改变文本
/// @param optionFont 文本字体
/// @param optionColor 文本颜色
- (NSMutableAttributedString *)attributedStringColor:(nullable UIColor *)textColor
                                                Font:(UIFont *)textFont
                                        OptionString:(NSString *)optionString
                                          OptionFont:(nullable UIFont *)optionFont
                                     foregroundColor:(UIColor *)optionColor
{
    NSRange totalRange  = [self range];
    NSRange subRang = [self rangeOfString:optionString];
    NSArray *configs = @[[YCAttributedStringConfig font:textFont range:totalRange],
                         [YCAttributedStringConfig foregroundColor:textColor range:totalRange],
                         [YCAttributedStringConfig font:optionFont?:textFont range:subRang],
                         [YCAttributedStringConfig foregroundColor:optionColor range:subRang]];
    return [self createAttributedStringAndConfig:configs];
}

/// 带行间距的可选富文本
/// @param textColor textColor description
/// @param textFont textFont description
/// @param optionString optionString description
/// @param optionFont optionFont description
/// @param optionColor optionColor description
/// @param spacing spacing description
- (NSMutableAttributedString *)attributedStringColor:(nullable UIColor *)textColor
                                                Font:(UIFont *)textFont
                                        OptionString:(NSString *)optionString
                                          OptionFont:(nullable UIFont *)optionFont
                                     foregroundColor:(UIColor *)optionColor
                                         lineSpacing:(CGFloat)spacing
{
    NSRange totalRange  = [self range];
    NSRange subRang = [self rangeOfString:optionString];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineSpacing = spacing;
    NSRange range = NSMakeRange(0, self.length);
    
    NSArray *configs = @[[YCAttributedStringConfig font:textFont range:totalRange],
                         [YCAttributedStringConfig foregroundColor:textColor range:totalRange],
                         [YCAttributedStringConfig font:optionFont?:textFont range:subRang],
                         [YCAttributedStringConfig foregroundColor:optionColor range:subRang],
                         [YCAttributedStringConfig attribute:NSParagraphStyleAttributeName value:style range:range]];
    
    
    return [self createAttributedStringAndConfig:configs];
}

- (NSMutableAttributedString *)attributedStringFont:(UIFont *)font spacing:(CGFloat)spacing {
    if (self.length == 0) {
        return nil;
    }
    
    NSMutableAttributedString *attr = [self attributedStringFont:font spacing:spacing textAlignment:NSTextAlignmentLeft];
    
    return attr;
}

- (NSMutableAttributedString *)attributedStringFont:(UIFont *)font spacing:(CGFloat)spacing textAlignment:(NSTextAlignment)textAlignment {
    if (self.length == 0) {
        return nil;
    }
    
    NSMutableAttributedString *astring = [[NSMutableAttributedString alloc] initWithString:self];
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineSpacing = spacing;
    style.alignment = textAlignment;
    
    NSRange range = NSMakeRange(0, self.length);
    [astring addAttribute:NSParagraphStyleAttributeName value:style range:range];
    [astring addAttribute:NSFontAttributeName value:font range:range];
    
    return astring;
}

@end
